<?php
error_reporting(0);
/**
 * Created by PhpStorm.
 * User: sunshubo
 * Date: 2017/8/25
 * Time: 17:49
 */

include '../inc/config/config.php';
$name = $_POST['name'];
$ip = $_SERVER["REMOTE_ADDR"];
//var_dump(urldecode($HTTP_RAW_POST_DATA));
$msg = explode('=', $HTTP_RAW_POST_DATA);
if (count($msg) < 2) {
    echo '{"num":0}';
    return;
}
$name = urldecode($msg[1]);
$md5 = md5($ip . $name);
$cn = mysqli_connect($MAC['db']['server'], $MAC['db']['user'], $MAC['db']['pass'], $MAC['db']['name'], $MAC['db']['port']);
//var_dump($MAC['db'],$cn);
if (!$cn) {
    echo '{"msg":"服务器开小差~1"}';
}

//设置编码格式
mysqli_query($cn, "set names utf8");

$sql = "select num from act_ticker where name = '" . $name . "'";
//var_dump($sql);
$result = mysqli_query($cn, $sql);
//var_dump($result->num_rows, $sql);
//return;


if ($result->num_rows == true) {
//    echo($result->fetch_row()[0]);
    echo '{"num":' . $result->fetch_row()[0] . '}';
} else {
    $num = rand(1, 100);
    $sql = "INSERT INTO act_ticker (id,name,num) VALUES('" . $md5 . "','" . $name . "',$num) ";
//    var_dump($sql, $name);
    $result = mysqli_query($cn, $sql);
    if (!$result) {
        echo '{"msg":"服务器开小差~2"}';
        return;
    }
    echo '{"num":' . $num . '}';
}